/*
动态规划求解：
dp[i][j]为到达点(i, j)的路线数目
由于只能往下或往右走
递推式:
dp[i][j] = dp[i-1][j] + dp[i][j-1]

初始情形:
obstacleGrid[i][j] == 1时, dp[i][j] = 0
dp[0][0] = 1
dp[i][0] = dp[i-1][0]
dp[0][j] = dp[0][j-1]

*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid)
    {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        if (m == 0 || n == 0 || obstacleGrid[0][0] == 1)
            return 0;
        //initial
        long long dp[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (obstacleGrid[i][j] == 1)
                    dp[i][j] = 0;
                else if (i == 0 && j == 0) {
                    dp[i][j] = 1;
                } else if (i == 0 || j == 0) {
                    if (i == 0)
                        dp[i][j] = dp[i][j - 1];
                    else
                        dp[i][j] = dp[i - 1][j];
                } else
                    dp[i][j] = -1;
            }
        }
        //dp
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (dp[i][j] == 0)
                    continue;
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }
};

int main(int argc, char const* argv[])
{
    Solution temp;
    vector<vector<int>> obstacleGrid;
    return 0;
}